package com.woniuxy.movie.product.controller;


import com.woniuxy.movie.commons.core.model.Result;
import com.woniuxy.movie.commons.web.util.OSSUtil;
import com.woniuxy.movie.product.exception.ProductException;
import com.woniuxy.movie.product.form.UpdateMovieStateForm;
import com.woniuxy.movie.product.form.productmovie.AddMovieForm;
import com.woniuxy.movie.product.service.ProductMovieService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 * 电影详情表 前端控制器
 * </p>
 *
 * @author 刘存益
 * @since 2023年11月17日
 */
@RestController
@RequestMapping("/productMovie")
@Api(tags = "电影相关接口")
public class ProductMovieController {

    @Autowired
    private ProductMovieService productMovieService;
    @Autowired
    private OSSUtil ossUtil;

    @PostMapping("/addImage.api")
    @ApiOperation("上传影片海报")
    public Result<String> addImage(MultipartFile movieImage) throws IOException {
        if (movieImage==null) throw new ProductException(2009,"传入图片不能为空");
        //获取上传图片的文件名
        String filename = movieImage.getOriginalFilename();
        //生成新的图片名称
        String name = UUID.randomUUID().toString().replaceAll("-","") + filename.substring(filename.lastIndexOf("."));
        //调用API上传
        String imagePath = ossUtil.uploadFile(movieImage.getInputStream(), name, "liucunyi");

        return Result.getResult(String.class).setData(imagePath).setMsg("上传影片海报成功");
    }
    @PostMapping("/add.api")
    @ApiOperation("新增影片")
    public Result<String> add(@RequestBody AddMovieForm form) throws IOException {
        productMovieService.add(form);
        return Result.getResult(String.class).setMsg("新增影片信息成功");
    }

    @PostMapping("/updateState.api")
    @ApiOperation("修改影片状态")
    public Result<String> updateMovieState(@RequestBody UpdateMovieStateForm form) throws IOException {

        productMovieService.updateMovieState(form);

        return Result.getResult(String.class).setMsg("修改影片状态成功");
    }

}

